package com.feicheng.common.config;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.constant.Constant;
import com.feicheng.persistence.entity.Vip;
import com.feicheng.persistence.service.IVipService;

import lombok.extern.slf4j.Slf4j;

@Configuration
@EnableScheduling
@Slf4j
public class VipCheckScheduleTask {
	
	@Autowired
	private IVipService iVipService;
	
	private QueryWrapper<Vip> vipQueryWrapper = new QueryWrapper<>();
	
	@Scheduled(cron = "0 0 1 * * ?")
    public void check() {
		LocalDateTime now = LocalDateTime.now();
		vipQueryWrapper.clear();
		vipQueryWrapper.eq(Constant.ENTITY.VIP.STATU, 1);
		vipQueryWrapper.le(Constant.ENTITY.VIP.ET, now);
		
		List<Vip> vips = iVipService.list(vipQueryWrapper);
		
		if (null != vips && vips.size() > 0) {
			for (Vip vip : vips) {
				vip.setStatu(0);
				vip.setEt(now);
				iVipService.updateById(vip);
			}
		}
    }

}
